#!/bin/bash
#
#********************************************************************
#Author:                songliangcheng
#QQ:                    2192383945
#Date:                  2023-02-18 11:26:15
#FileName：             install-salt-minion.sh
#URL:                   http://blog.mykernel.cn
#Description：          curl -SsL https://gitee.com/slcnx/tools/raw/master/scripts/saltstack/install-salt-minion.sh | sed 's/\r//' | bash -s -- --interface 192.168.0.82 --master 192.168.0.82 [ --id 192.168.0.82 ]
#Copyright (C):        2023 All rights reserved
#********************************************************************
#描述:
#--bind-ip 选项必须传递
#--id 选项可以省略
#--master 选项必须传递

source <(curl -sSLf https://gitee.com/slcnx/tools/raw/master/parse_cmd.sh | sed 's/\r//g')

CONFIG='

key    ,argument    ,isempty    ,desc    

--interface    ,interface    ,0    ,工作在哪个接口

--id    ,id    ,1    ,分配的id为, 默认interface

--master    ,master    ,0    ,master地址

--ret_port , ret_port, 1,  minion将结果提交给master，master监听此端口. 默认4506
'

parse_cmd $@

echo $interface $master

: ${id:=$interface}
: ${ret_port:=4506}

echo "$id "

# Download key
#https://docs.saltproject.io/salt/install-guide/en/latest/topics/install-by-operating-system/ubuntu.html
sudo curl -fsSL -o /etc/apt/keyrings/salt-archive-keyring.gpg https://repo.saltproject.io/salt/py3/ubuntu/22.04/amd64/SALT-PROJECT-GPG-PUBKEY-2023.gpg
echo "deb [signed-by=/etc/apt/keyrings/salt-archive-keyring.gpg arch=amd64] https://repo.saltproject.io/salt/py3/ubuntu/22.04/amd64/latest jammy main" | sudo tee /etc/apt/sources.list.d/salt.list
apt-get update
sudo apt-get install salt-master -y
sudo apt-get install salt-minion -y
sudo apt-get install salt-ssh -y
sudo apt-get install salt-syndic -y
sudo apt-get install salt-cloud -y
sudo apt-get install salt-api -y

cat > /etc/salt/minion.d/default.conf <<EOF
interface: ${interface}
master: ${master}
master_port: $ret_port
id: ${id}
EOF
systemctl restart salt-minion.service
